<template>
	<view class="bg-white text-center">
		<view class="padding">
			<view class="grid col-2 solids" style="border-radius: 10px;">
				<view class="padding-tb-xl ">
					<view class="text-black" style="font-size: 80rpx;" v-if="member">
						{{member.dwqs || 0}}
					</view>
					<view class="" style="font-size: 40rpx;color: #7a7883">
						待我签署
					</view>
				</view>
				<view class="padding-tb-xl">
					<view class="text-black solids-left" style="font-size: 80rpx;">
						{{member.trqs || 0}}
					</view>
					<view class="solids-left" style="font-size: 40rpx;color: #7a7883;">
						<view class="">
							待他人签署
						</view>
					</view>
				</view>
			</view>

			<view class="cu-list menu-avatar solids margin-top" style="border-radius: 5px;">
				<view class="cu-item arrow" @click="go('message')">
					<view class="cu-avatar round bg-blue text-xxl"><text class="cuIcon-notificationfill"></text></view>
					<view class="content" style="left: 100rpx;">
						<block v-for="(item,index) in member.messagelist" :key="index">
							<view class="flex">
								<view class="text-cut">
									<text class="cuIcon-title margin-right-xs"></text>
									{{item.title}}
								</view>
							</view>
						</block>
						<view class="flex" v-if="member.messagelist.length < 1">
							<view class="text-cut">
								<text class="cuIcon-title margin-right-xs"></text>
								暂无新消息
							</view>
						</view>
					</view>
					<view class="action">
						<text class="cu-tag bg-red">{{member.code == 1000001 ? 0 : member.message}}</text><text class="cuIcon-right"></text>
					</view>
				</view>
			</view>
			<template v-if="!user.salesman_mobile">
				<view class="text-center" style="padding-top: 100rpx;">
					<view class="text-black text-bold" style="font-size: 32rpx;">
						您当前还未登陆，请先登陆
					</view>
					<view class="padding">
						<button class="cu-btn block bg-blue margin-tb-sm lg"
							@click="$u.route('/pages/member/member')">立即登陆</button>
					</view>
				</view>
			</template>
			<template v-else-if="member.status != 'AUTH_SUCCESS' || member.code == '1000001'">
				<view class="text-center" style="padding-top: 100rpx;">
					<view class="text-black text-bold" style="font-size: 32rpx;">
						您当前账号没有认证主体
					</view>
					<view class="text-gray margin-tb-sm" style="font-size: 28rpx;">
						为保证签署的有效性，签署前请完成主体的实名认证！
					</view>
					<view class="padding">
						<button class="cu-btn block bg-blue margin-tb-sm lg" @click="isrenzheng = true">立即认证</button>
					</view>
				</view>
			</template>
			<template v-else-if="company.length < 1 || renzheng">
				<view class="text-center" style="padding-top: 100rpx;">
					<view class="text-black text-bold" style="font-size: 32rpx;">
						您当前账号没有认证企业主体
					</view>
					<view class="text-gray margin-tb-sm" style="font-size: 28rpx;">
						为保证签署的有效性，签署前请完成主体的企业认证！
					</view>
					<view class="padding">
						<button class="cu-btn block bg-blue margin-tb-sm lg" @click="go('h5page')">立即企业认证</button>
					</view>
				</view>
			</template>
			<template v-else>
				<view class="margin-top">
					<view class="cu-bar bg-white solids-bottom">
						<view class="action">
							<text
								style="border-bottom: 4px solid #0081FF;height: 100rpx;line-height: 100rpx;">最近文件</text>
						</view>
						<view class="action" @click="$u.route({url:'/pages/member/file',type:'tab'})">
							<text style="height: 100rpx;line-height: 100rpx;">更多</text>
						</view>
					</view>
				</view>
				<block v-for="(item,index) in list" :key="index">
					<view class="bg-gray radius margin-top padding-tb-sm" style="position:relative"
						@click="contractsignurl(item.id)">
						<view class="cu-tag bg-blue radius" style="position: absolute;top: 0px;right: 0px;">
							{{item.status}}
						</view>
						<view class="" style="width:95%;">
							<view class="cu-timeline" style="background-color: unset;">
								<view class="cu-item cuIcon-moneybag text-left" style="padding: 4rpx 20rpx 0rpx 70rpx;">
									<text class="text-xxl text-bold">{{item.subject}}</text>
								</view>
								<view class="cu-item text-left text-grey cuIcon-file"
									style="padding: 20rpx 20rpx 0rpx 70rpx;">
									发件方：{{item.json.tenantName}}
								</view>
								<view class="cu-item text-left text-grey cuIcon-countdown"
									style="padding: 20rpx 20rpx 0rpx 70rpx;">
									签发日期：{{item.createTime}}
								</view>
							</view>
						</view>
					</view>
				</block>
			</template>
		</view>
		<view class="cu-modal bottom-modal" :class="modalName=='bottomModal'?'show':''" @tap="hideModal">
			<view class="cu-dialog" @tap.stop="">
				<view class="cu-bar bg-white">
					<view class="action text-blue" @tap="hideModal">取消</view>
				</view>
				<view class="padding">
					<button class="cu-btn block line-blue margin-tb-sm lg" @click="hetong('liulan')">浏览合同</button>
					<button class="cu-btn block bg-blue margin-tb-sm lg" @click="hetong('qianshu')">签署合同</button>
					<button open-type="share" class="cu-btn block line-red margin-tb-sm lg">转发合同</button>
				</view>
			</view>
		</view>
		<view class="cu-modal bottom-modal" :class="isrenzheng?'show':''" @click="isrenzheng = false">
			<view class="cu-dialog" @click.stop="">
				<view class="cu-bar bg-white solids-bottom">
					<view class="action" @click="isrenzheng = false">
						<text class="cuIcon-close"></text>
					</view>
					<view class="content text-bold">
						选择认证主体
					</view>
				</view>
				<view class="padding-xl bg-white">
					<view class="cu-list menu-avatar solids radius" @click="go('authurl')"
						v-if="member.status == 'AUTH_FAILURE'">
						<view class="cu-item">
							<view class="cu-avatar round lg" style="background-color: unset;border:1px solid #0081FF">
								<image src="../../static/34.png" mode="" style="width: 68rpx;height: 40rpx;"></image>
							</view>
							<view class="content" style="width: 100%;">
								<view class="text-black text-bold">个人实名认证</view>
								<view class="text-gray text-sm flex">
									<view class="text-cut">
										点击进去“静坐”个人实名认证
									</view>
								</view>
							</view>
						</view>
					</view>

					<view class="cu-list menu-avatar solids radius" @click="go('h5page')">
						<view class="cu-item">
							<view class="cu-avatar round lg" style="background-color: unset;border:1px solid #0081FF">
								<image src="../../static/36.png" mode="" style="width: 68rpx;height: 40rpx;"></image>
							</view>
							<view class="content" style="width: 100%;">
								<view class="text-black text-bold">企业主体认证</view>
								<view class="text-gray text-sm flex">
									<view class="text-cut">
										通过企业实名认证，授权成为企业员工
									</view>
								</view>
							</view>
						</view>
					</view>

				</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="text-center padding-lr-lg bg-white">
					<image src="../../static/32.png" mode="widthFix" style="width: 100%;"></image>
					<view class="text-black text-bold" style="font-size: 32rpx;">
						{{member.name}} 欢迎您加入瀚生电子签
					</view>
					<view class="text-gray margin-tb-sm" style="font-size: 28rpx;">
						为了您证签署的合法有效性 您须完成主体的实名认证，才能签署哦
					</view>
					<view class="padding">
						<button class="cu-btn block bg-blue margin-tb-sm lg"
							@click="isrenzheng = true,modalName = null">添加认证主体</button>
					</view>
				</view>
			</view>
		</view>
		<view class="cu-tabbar-height"></view>
		<view class="cu-bar tabbar bg-white shadow foot">
			<navigator class="action text-blue" open-type="switchTab" url="/pages/index/index">
				<view class='cuIcon-homefill'></view>
				<view>首页</view>
			</navigator>
			<navigator open-type="switchTab" url="/pages/member/file" class="action">
				<view class='cuIcon-file'></view>
				<view :class="PageCur=='component'?'text-green':''">文件</view>
			</navigator>
			<navigator url="/pages/member/index" class="action">
				<view class='cuIcon-people'></view>
				<view :class="PageCur=='plugin'?'text-green':''">我的</view>
			</navigator>
		</view>
	</view>
</template>

<script>
	var that
	export default {
		data() {
			return {
				modalName: null,
				isrenzheng: false,
				renzheng: true,
				member: {},
				list: [],
				company: [],
				user: {},
				curid: '',
				hetongid: ''
			}
		},
		onLoad(e) {
			that = this
			that.hetongid = e.hetongid
		},
		onShow() {
			if (that.hetongid) {
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						let url = 'https://sigh.qdhansen.com/contractdetail/' + that.hetongid +
							'?code=' + loginRes.code
						uni.navigateTo({
							url: '/pages/center/index?url=' + encodeURIComponent(JSON.stringify(url)),
							success() {
								that.hetongid = ''
							}
						})
					}
				})
				return false
			}
			that.$u.post('index/member').then(res => {
				that.user = res
			})
			that.init()
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: that.user.name + '发您一个合同需要签署',
				path: '/pages/index/index?hetongid='+this.curid
			}
		},
		methods: {
			hideModal(e) {
				this.modalName = null
			},
			init() {
				that.company = []
				that.$u.post('qiyue/user').then(res => {
					if (res.code == 1000001 && !uni.getStorageSync('islogin')) {
						uni.reLaunch({
							url: '../member/member'
						})
						return false
					}
					that.member = res
					if (res.status == 'AUTH_FAILURE') {
						this.modalName = 'Modal'
					}
					that.$u.post('index/company').then(res => {
						if (res.code == 1000001 && !uni.getStorageSync('islogin')) {
							uni.reLaunch({
								url: '../member/member'
							})
							return false
						}
						let company = []
						for (let i in res) {
							if (res[i].status.result == 'PASSED') {
								that.renzheng = false
							}
							that.company.push(res[i].name);
						}
					})
					that.$u.post('qiyue/contractlist',{"status":"SIGNING"}).then(res => {
						that.list = res
					})
				})
			},
			hetong(e) {
				let id = this.curid
				if (e == 'liulan') {
					that.$u.post('qiyue/contractsignurl', {
						id: id,
						'page': 'viewurl'
					}).then(res => {
						if (res.code !== 0) {
							uni.showToast({
								title: res.message,
								duration: 2000,
								icon: "none"
							});
							return false
						}
						uni.navigateTo({
							url: '/pages/center/index?url=' + encodeURIComponent(JSON
								.stringify(res.viewUrl))
						})
					})
				} else {
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							let url = 'https://sigh.qdhansen.com/contractdetail/' + id +
								'?code=' + loginRes.code
							uni.navigateTo({
								url: '/pages/center/index?url=' +
									encodeURIComponent(JSON.stringify(url))
							})
						}
					})
				}
			},
			contractsignurl(e) {
				this.modalName = 'bottomModal'
				this.curid = e
			},
			go(url) {
				if (url == 'message') {
					wx.requestSubscribeMessage({
						tmplIds: ['MFb3FPAFmka9C69W9O2VC35CrwQRX2eBnz7ZICC0Wac'],
						success(res) {
							uni.navigateTo({
								url: '/pages/index/message'
							})
						}
					})
				}
				if (url == 'h5page') {
					if (this.member.status == 'AUTH_FAILURE') {
						uni.showToast({
							title: '您需要先完成个人实名认证',
							duration: 2000,
							icon: "none"
						});
						return false
					}
					var company = []
					if (that.company.length < 1) {
						company = ['添加企业']
					} else {
						company = that.company
					}
					uni.showActionSheet({
						itemList: company,
						success: function(res) {
							wx.requestSubscribeMessage({
								tmplIds: ['MFb3FPAFmka9C69W9O2VC35CrwQRX2eBnz7ZICC0Wac'],
								success(val) {
									if (company[res.tapIndex] == '添加企业') {
										uni.navigateTo({
											url: '/pages/company/create'
										})
									} else {
										that.$u.post('qiyue/h5page', {
											name: company[res.tapIndex]
										}).then(res => {
											if (res.code !== 0) {
												uni.showToast({
													title: res.message,
													duration: 2000,
													icon: "none"
												});
												return false
											}
											uni.navigateTo({
												url: '/pages/center/index?url=' +
													encodeURIComponent(JSON
														.stringify(res.authurl))
											})
										})
									}
								}
							})
						},
						fail: function(res) {
							console.log(res.errMsg);
						}
					});
				} else {
					that.$u.post('qiyue/' + url, {}).then(res => {
						uni.navigateTo({
							url: '/pages/center/index?url=' + encodeURIComponent(JSON.stringify(res
								.result))
						})
					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.cu-timeline>.cu-item::before {
		font-family: "cuIcon";
		display: block;
		position: absolute;
		top: 36rpx;
		z-index: 9;
		background-color: unset;
		width: 50rpx;
		height: 50rpx;
		text-align: center;
		border: none;
		line-height: 11rpx;
		left: 14rpx;
	}

	.cu-timeline>.cu-item::after {
		content: "";
		display: block;
		position: absolute;
		width: 1rpx;
		background-color: #ddd;
		left: 39rpx;
		height: 100%;
		top: 0;
		z-index: 8;
	}
</style>
